/**
* 基础订单 订单管理 退款管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'#'}">订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>退款管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 列表 -->
    <div class="H10"></div>
    <div class="zhang">
      <el-tabs type="border-card" v-model="chargebackInfo.chargeback_status">
        <el-tab-pane label="待处理">
          <!-- :data="wait_data" -->
          <el-table border style="width: 100%;" :data="listData">
            <el-table-column align="center" type="index" prop="chargeback_id" sortable label="序号" width="60">
            </el-table-column>
            <el-table-column align="center" prop="indent_id" label="订单号" width="80">
            </el-table-column>
            <el-table-column align="center" prop="chargeback_id" label="退款单号" width="80">
            </el-table-column>
            <el-table-column align="center" prop="nickname" label="用户昵称" width="100">
            </el-table-column>
            <el-table-column align="center" prop="chargeback_price" sortable label="退款金额" width="120">
            </el-table-column>
            <el-table-column align="center" prop="chargeback_cause" label="退款原因" width="160"></el-table-column>
            <!-- <el-table-column align="center" label="状态" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.status==0">退款中</span>
                <span v-else-if="scope.row.status==1">已退款</span>
                <span v-else>已驳回</span>
              </template>
            </el-table-column> -->
            <el-table-column align="center" sortable prop="add_time" label="增加时间" width="150">
              <template slot-scope="scope">
                <div>{{scope.row.add_time|timestampToTime}}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" sortable prop="update_time" label="修改时间" width="150">
              <template slot-scope="scope">
                <div>{{scope.row.update_time|timestampToTime}}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="username" label="操作员" width="100">
            </el-table-column>
            <el-table-column align="center" label="操作" min-width="260">
              <template slot-scope="scope">
                <el-button type="text" size="mini" @click="tk(scope.row)">查看详情</el-button>
                <el-button type="warning" size="mini" @click="updateChargeback(scope.row)">确认退款</el-button>
                <!-- <el-button type="danger" size="mini" @click="tkbh(scope.row)">驳回申请</el-button> -->
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="已处理">
          <!-- :data="done_data" -->
          <el-table border style="width: 100%;" :data="listData">
            <el-table-column align="center" type="index" prop="chargeback_id" sortable label="序号" width="80">
            </el-table-column>
            <el-table-column align="center" prop="indent_id" label="订单号" width="80">
            </el-table-column>
            <el-table-column align="center" prop="chargeback_id" label="退款单号" width="80">
            </el-table-column>
            <el-table-column align="center" prop="nickname" label="用户昵称" width="120">
            </el-table-column>
            <el-table-column align="center" sortable prop="chargeback_price" label="退款金额" width="120">
            </el-table-column>
            <el-table-column align="center" prop="chargeback_cause" label="退款原因" width="200"></el-table-column>
            <el-table-column label="状态" width="120">
              <template slot-scope="scope">
                <!-- <span v-if="scope.row.status==0">退款成功</span>
                <span v-else-if="scope.row.status==1">退款失败</span>
                <span v-else>未知</span> -->
                <span>退款成功</span>
              </template>
            </el-table-column>
            <el-table-column align="center" sortable prop="add_time" label="增加时间" width="160">
              <template slot-scope="scope">
                <div>{{scope.row.add_time|timestampToTime}}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" sortable prop="update_time" label="修改时间" width="160">
              <template slot-scope="scope">
                <div>{{scope.row.update_time|timestampToTime}}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="username" label="操作员" min-width="100">
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
  </div>
</template>

<script>
  import {
    ChargebackList,
    ChargebackAdd,
    ChargebackUpdate,
    ChargebackDelete,
  } from '../../api/adminMG'
  import {
    getStore
  } from '../../utils/util'
  import Pagination from '../../components/Pagination'
  export default {
    data() {
      return {
        category: [],
        show_sku: 1,
        nshow: true, //switch开启
        fshow: false, //switch关闭
        loading: false, //是显示加载
        editFormVisible: false, //控制编辑页面显示与隐藏
        title: '添加',
        editForm: {
        },
        formInline: {
          page: 1,
          limit: 10,
          varLable: '',
          varName: '',
          token: localStorage.getItem('logintoken')
        },
        chargebackInfo:{
          chargeback_status:0,
        },
        userparm: [], //搜索权限
        listData: [], //用户数据
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        }
      }
    },
    // 注册组件
    components: {
      Pagination
    },
    /**
     * 数据发生改变
     */

    /**
     * 创建完毕
     */
    created() {
      // this.getdata(this.formInline);
      this.getChargebackList(this.chargebackInfo);
    },
    watch:{
      'chargebackInfo.chargeback_status':function(newVal,oldVal){
        // console.log('newVal',newVal,'oldVal',oldVal)
        if(newVal != oldVal){
          // console.log("请求退款订单列表")
          this.chargebackInfo.chargeback_status = newVal;
          this.getChargebackList(this.chargebackInfo)
        }
      }
    },
    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取退款订单列表（默认状态为1——待处理）
      getChargebackList(parameter) {
        this.loading = true
        ChargebackList(parameter)
          .then(res => {
            this.loading = false
            if (res.status == 0) {
              this.$message({
                type: 'success',
                message: "获取数据成功"
              })
              console.log("请求退款订单列表", res.data.chargebackList,"退款订单状态",res.data.chargeback_status);
              this.listData = res.data.chargebackList;
              if (this.listData.length > 0) {
                this.show_input = true
              } else {
                this.$message({
                  showClose: true,
                  message: "该订单状态下暂无订单数据",
                  type: "warning"
                });
              }
              //   // 分页赋值
              //   this.pageparm.currentPage = this.formInline.page
              //   this.pageparm.pageSize = this.formInline.limit
              //   this.pageparm.total = res.count
            } else {
              this.$message({
                type: 'warning',
                message: "获取退款订单列表失败"
              })
            }
          })
          .catch(err => {
            this.loading = false
            this.$message.error('退款订单加载失败，请稍后再试！')
          })
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.limit = parm.pageSize
        this.getdata(this.formInline)
      },
      // 搜索事件
      search() {
        this.getdata(this.formInline)
      },
      //显示编辑界面
      handleEdit: function(index, row) {
        this.editFormVisible = true
        if (row != undefined && row != 'undefined') {
          this.title = '编辑'
          this.editForm.staff_id = row.staff_id;
        } else {
          this.title = '添加'
          this.editForm = {}
        }
      },
      // 确认退款
      updateChargeback(row) {
        this.$confirm('确定要退款吗?', '信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            this.editForm.chargeback_id = row.chargeback_id;
            this.editForm.staff_id = getStore('staffInfo').staff_id;
            console.log("editForm",this.editForm);
            ChargebackUpdate(this.editForm)
              .then(res => {
                console.log("订单退款", res)
                if (res.data.result) {
                  this.$message({
                    type: 'success',
                    message: "订单退款成功"
                  })
                  this.getChargebackList(this.chargebackInfo);
                } else {
                  this.$message({
                    type: 'warning',
                    message: "订单退款失败"
                  })
                }
              })
              .catch(err => {
                this.loading = false
                this.$message.error('订单退款失败，请稍后再试！')
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消退款'
            })
          })
      },
      // 关闭编辑、增加弹出框
      closeDialog() {
        this.editFormVisible = false
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }

  .BH10 {
    height: 10px;
    background-color: #f0f0f0
  }

  .zhang {
    text-align: left;
    margin: 40px 0px 30px 20px;
  }
</style>
